#define _CRT_SECURE_NO_WARNINGS
#include "list.h"

void SeqListInit(SeqList* ps) 
{
	SLDateType* temp = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	if (temp == NULL)
	{
		perror("malloc failed");
		return;
	}
	ps->a = temp;
	ps->capacity = 4;
	ps->size = 0;
}

void SeqListDestroy(SeqList* ps) 
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

void SeqListPrint(SeqList* ps)
{
	if (ps->size == 0)
		return;
	int i;
	for (i = 0; i < ps->size; i++)
		printf("%d ", ps->a[i]);
	printf("\n");
}

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	if (ps->capacity == ps->size)
	{
		SLDateType* temp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
		if (temp == NULL)
		{
			perror("realloc failed");
			return;
		}
		ps->a = temp;
		ps->capacity *= 2;
	}
	ps->a[ps->size++] = x;
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	if (ps->capacity == ps->size)
	{
		SLDateType* temp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
		assert(temp);
		if (temp == NULL)
		{
			perror("realloc failed");
			return;
		}
		ps->a = temp;
		ps->capacity *= 2;
	}
	int i;
	for (i = ps->size - 1; i >= 0; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
	if (ps->size == 0)
		return;
	int i;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

void SeqListPopBack(SeqList* ps)
{
	if (ps->size == 0)
		return;
	ps->size--;
}

int SeqListFind(SeqList* ps, SLDateType x) 
{
	int i;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return 0;
}

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	if (ps->capacity == ps->size)
	{
		SLDateType* temp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
		if (temp == NULL)
		{
			perror("realloc failed");
			return;
		}
		ps->a = temp;
		ps->capacity *= 2;
	}
	int i;
	for (i = ps->size; i > pos; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[i] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)
{
	int i;
	for (i = pos; i < ps->size; i++)
	{
		ps->a[i - 1] = ps->a[i];
	}
	ps->size--;
}